const A = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: []
  },
  goOrders(e) {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower' })
    }
    let type = A.C(e).type;
    let index = A.C(e).index;
    wx.navigateTo({ url: `/pages/mall/orderList/orderList?type=${type}&index=${index}`})
  },
  goPage(e) {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower' })
    }
    let page = A.C(e).page;
    wx.navigateTo({ url: page })
  },
  // 排名页面
  toRanking() {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower' })
    }
    wx.navigateTo({
      url: '/pages/my/ranking/ranking',
    })
  },
  // 规则页面
  toRule(e) {
    let id = A.C(e).id;
    wx.navigateTo({
      url: `/pages/my/rule/rule?id=${id}`
    })
  },
  toLogin() {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower' })
    }
    if (wx.getStorageSync("merchantInfo")) {
      let merchantInfo = JSON.parse(wx.getStorageSync('merchantInfo'));
      let merchantId = merchantInfo.id;
      console.log(merchantInfo);
      wx.navigateTo({
        url: `/pages/my/merchantDetail/merchantDetail?merchantId=${merchantId}`
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/login/login'
      })
    }
  },

  login() {
    if (!wx.getStorageSync('userId')) {
      return;
    }
    let userInfo = JSON.parse(wx.getStorageSync('userInfo'));
    let config = {
      avatar: userInfo.avatar,
      gender: userInfo.gender,
      nickname: userInfo.nickname,
      openId: userInfo.openId
    }
    A.updata.getLogin(config).then(res => {
      console.log(res);
      this.setData({
        userInfo: res.results
      })
      wx.setStorageSync('userInfo', JSON.stringify(res.results));
    })
  },
  toImpower() {
    wx.navigateTo({ url: '/pages/index/impower/impower' })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
      if (wx.getStorageSync('openId')) {
          this.login();
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function () {
      if (!wx.getStorageSync('openId')) {
          wx.navigateTo({ url: '/pages/index/impower/impower' })
      }
      this.login();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})